import { serve } from '@hono/node-server'
import { Context, Hono } from 'hono'
import { logger } from 'hono/logger'
import { prettyJSON } from 'hono/pretty-json'
import { cors } from 'hono/cors'
import { errorHandler, notFound } from './middlewares/errorMiddlewares'
import { TrRoute } from './routes'
import { env } from 'hono/adapter'
// import { createMiddleware } from 'hono/factory'
// import { serveStatic } from '@hono/node-server/serve-static'


// Fix BigInt issue
// https://github.com/GoogleChromeLabs/jsbi/issues/30
(BigInt.prototype as any).toJSON = function () {
    return this.toString();
}


const app = new Hono()
app.use('*', logger(), prettyJSON())
app.use(
    '*',
    cors({
        origin: '*',
        allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
    })
)

// Home Route
app.get('/', (c: Context) => c.text('Welcome to the API!'))
app.get('/env', (c: any) => {
    // NAME is process.env.NAME on Node.js or Bun
    // NAME is the value written in `wrangler.toml` on Cloudflare
    const { APP_NAME } = env(c)

    // return c.text(`APP_NAME: ${JSON.stringify( env(c, 'deno'))} ${APP_NAME2}`)
    return c.text(`APP_NAME: ${APP_NAME}`)
})

app.route('/tr', TrRoute)



app.onError((err: any, c: Context) => {
    const error = errorHandler(c)
    return error
})
app.notFound((c: Context) => {
    const error = notFound(c)
    return error
})



serve(app)
// serve({
//     fetch: app.fetch,
//     port: 8787, // Port number, default is 3000
// })